package com.leaf.collegeidleapp.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.leaf.collegeidleapp.R;
import com.leaf.collegeidleapp.StarActivity;
import com.leaf.collegeidleapp.bean.Collection;
import com.leaf.collegeidleapp.bean.Star;
import com.leaf.collegeidleapp.toucheventtest.SideslipListView;
import com.leaf.collegeidleapp.util.MyCollectionDbHelper;
import com.leaf.collegeidleapp.util.StarDBHelper;
import com.leaf.collegeidleapp.StarActivity;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class StarAdapter extends BaseAdapter {

    private Context context;
    private LayoutInflater layoutInflater;
    private List<Star> stars = new ArrayList<>();
    StarActivity starActivity = new StarActivity();

    HashMap<Integer, View> location = new HashMap<>();

    public StarAdapter(Context context) {
        this.context = context;
        layoutInflater = LayoutInflater.from(context);
    }

    public StarAdapter() {

    }

    public void setData(List<Star> stars) {
        this.stars = stars;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return stars.size();
    }

    @Override
    public Object getItem(int position) {
        return stars.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (null == convertView) {
            convertView = layoutInflater.inflate(R.layout.layout_star, null);
            Star star = (Star) getItem(position);
            viewHolder = new ViewHolder(convertView,star);
            //viewHolder.textView = (TextView) convertView.findViewById(R.id.textView);
            viewHolder.txtv_delete = (TextView) convertView.findViewById(R.id.txtv_delete);
            convertView.setTag(viewHolder);

        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        //viewHolder.textView.setText(mDataList.get(position));
        final int pos = position;
        viewHolder.txtv_delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                StarAdapter adapter = new StarAdapter();
                StarDBHelper dbHelper = null;
                //根据商品名称,商品描述和价格执行删除操作
                Star star = (Star)adapter.getItem(pos);
                //删除收藏商品项
                dbHelper.deleteMyStar(star.getPhone());
                stars.remove(pos);
                notifyDataSetChanged();
                starActivity.mSideslipListView.turnNormal();
            }
        });
        return convertView;
    }

    //定义静态类,包含每一个item的所有元素
    static class ViewHolder {
        public TextView txtv_delete;
        ImageView ivStar;
        TextView tvName,tvPhone;

        public ViewHolder(View itemView,Star star) {
            tvName = itemView.findViewById(R.id.tv_name);
            tvPhone = itemView.findViewById(R.id.tv_phone);
            ivStar = itemView.findViewById(R.id.iv_star);
            tvName.setText(star.getStuId());
            tvPhone.setText(star.getPhone());
        }
    }
}
